1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.LinkedHashMap;
22  
23  /**
24   * A {@code Multiset} implementation with predictable iteration order. Its
25   * iterator orders elements according to when the first occurrence of the
26   * element was added. When the multiset contains multiple instances of an
27   * element, those instances are consecutive in the iteration order. If all
28   * occurrences of an element are removed, after which that element is added to
29   * the multiset, the element will appear at the end of the iteration.
30   * 
31   * <p>See the Guava User Guide article on <a href=
32   * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multiset">
33   * {@code Multiset}</a>.
34   *
35   * @author Kevin Bourrillion
36   * @author Jared Levy
37   * @since 2.0 (imported from Google Collections Library)
38   */
39  @GwtCompatible(serializable = true, emulated = true)
40  @SuppressWarnings("serial") // we're overriding default serialization
41  public final class LinkedHashMultiset<E> extends AbstractMapBasedMultiset<E> {
42  
43    /**
44     * Creates a new, empty {@code LinkedHashMultiset} using the default initial
45     * capacity.
46     */
47    public static <E> LinkedHashMultiset<E> create() {
48      return new LinkedHashMultiset<E>();
49    }
50  
51    /**
52     * Creates a new, empty {@code LinkedHashMultiset} with the specified expected
53     * number of distinct elements.
54     *
55     * @param distinctElements the expected number of distinct elements
56     * @throws IllegalArgumentException if {@code distinctElements} is negative
57     */
58    public static <E> LinkedHashMultiset<E> create(int distinctElements) {
59      return new LinkedHashMultiset<E>(distinctElements);
60    }
61  
62    /**
63     * Creates a new {@code LinkedHashMultiset} containing the specified elements.
64     *
65     * <p>This implementation is highly efficient when {@code elements} is itself
66     * a {@link Multiset}.
67     *
68     * @param elements the elements that the multiset should contain
69     */
70    public static <E> LinkedHashMultiset<E> create(
71        Iterable<? extends E> elements) {
72      LinkedHashMultiset<E> multiset =
73          create(Multisets.inferDistinctElements(elements));
74      Iterables.addAll(multiset, elements);
75      return multiset;
76    }
77  
78    private LinkedHashMultiset() {
79      super(new LinkedHashMap<E, Count>());
80    }
81  
82    private LinkedHashMultiset(int distinctElements) {
83      // Could use newLinkedHashMapWithExpectedSize() if it existed
84      super(new LinkedHashMap<E, Count>(Maps.capacity(distinctElements)));
85    }
86  }
87